package com.tomorrowbbs.controller.base;

import com.tomorrowbbs.domain.constants.Constants;
import com.tomorrowbbs.domain.dto.SessionWebUserDto;
import com.tomorrowbbs.domain.vo.PaginationResultVo;
import com.tomorrowbbs.domain.vo.ResponseVo;
import com.tomorrowbbs.domain.enums.ResponseCodeEnum;
import com.tomorrowbbs.exception.BusinessException;
import com.tomorrowbbs.utils.CopyTools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class ABaseController {
    protected static final String STATUC_SUCCESS = "success";
    protected static final String STATUC_ERROR = "error";

    protected <T>ResponseVo getSuccessResponseVo(T t){
        ResponseVo<T> responseVo = new ResponseVo<>();
        responseVo.setStatus(STATUC_SUCCESS);
        responseVo.setCode(ResponseCodeEnum.CODE_200.getCode());
        responseVo.setInfo(ResponseCodeEnum.CODE_200.getMsg());
        responseVo.setData(t);
        return responseVo;
    }

    protected <T> ResponseVo getBusinessErrorResponseVo(BusinessException e,T t){
        ResponseVo vo = new ResponseVo();
        vo.setStatus(STATUC_ERROR);
        if (e.getCode() == null){
            vo.setCode(ResponseCodeEnum.CODE_600.getCode());
        }else {
            vo.setCode(e.getCode());
        }
        vo.setInfo(e.getMessage());
        vo.setData(t);
        return vo;
    }

    protected <T> ResponseVo getServerErrorResponseVo(T t){
        ResponseVo vo = new ResponseVo();
        vo.setStatus(STATUC_ERROR);
        vo.setCode(ResponseCodeEnum.CODE_500.getCode());
        vo.setInfo(ResponseCodeEnum.CODE_500.getMsg());
        vo.setData(t);
        return vo;
    }

    protected String getIpAddr(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null || ip.length() != 0 || !"unknown".equalsIgnoreCase(ip)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("HTTP-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    protected SessionWebUserDto getUserInfoFromSession(HttpSession session){
        SessionWebUserDto sessionWebUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        return sessionWebUserDto;
    }

    protected <S, T>PaginationResultVo<T> convert2PaginationVO(PaginationResultVo<S> result, Class<T> clazz){
        PaginationResultVo<T> resultVo = new PaginationResultVo<>();
        resultVo.setList(CopyTools.copyList(result.getList(), clazz));
        resultVo.setPageNo(result.getPageNo());
        resultVo.setPageSize(result.getPageSize());
        resultVo.setPageTotal(result.getPageTotal());
        resultVo.setTotalCount(result.getTotalCount());
        return resultVo;
    }

}















